<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/5/16 0016
 * Time: 11:12
 */

namespace Wx\Controller;
use Think\Controller;

class ExchangeReceiveController extends CommonController
{
    //进入验证核销权限
    public function Index(){
        //dump($u_id);
        $order_sn = $_GET['sn'];//exchange_order表订单号
        $judge = $this->judge($order_sn);
        //是否有核销权限
        if($judge){
            $this->redirect("ExchangeReceive/cancel",['sn'=>$order_sn]);
        }else{
            $this->error("无操作权限","http://www.xiangtws.com/Wx/User/stores.html");
        }
    }//end fun index()

    //权限判断方法
    public function judge($order_sn){
        $u_id = session("u_id");//登录用户id
        $o_c_id = M("exchange_order")->where(array("order_sn"=>$order_sn))->getField('c_id');//用户小区id

        //获取店铺店主id和店铺id
        $store = M("shop_store")->where("c_id = {$o_c_id}")->field("id,u_id")->find();//门店信息

        if ($store){
//            dump($order_saler);
            //登录用户是否为该订单店主
            $u_id_str = array();
            $u_id_str[0]=$store['u_id'];
            //获取店铺店员id
            $clerk = M("shop_saler")->where("storeid = {$store['id']}")->field('u_id')->select();
            if($clerk){
                foreach ($clerk as $k => $v){
                    $u_id_str[$k+1]=$v['u_id'];
                }
            }

//            $u_id_str[]=1391;

            if(in_array($u_id,$u_id_str)){
                return true;
            }else{
                return false;
            }
        }//end if $order_saler
    }//end fun judge()

    //核销界面
    public function cancel(){
        $order_sn = $_REQUEST['sn'];//exchange_order表订单号
        $order = M("exchange_order")->where(array("order_sn"=>$order_sn))->find();//订单信息
        $goods = M("exchange_goods")->where("id = {$order['g_id']}")->find();//商品信息

        $order['money'] = $order['total_fee']>0?$order['total_fee']:$order['card_fee'];//支付金额（元）

        $this->assign("order",$order);
        $this->assign("goods",$goods);
        $this->display('cancel');
    }//end fun cancel()

    //核销
    public function receive(){
//        $this->ajaxReturn($_POST,"JSON");return;
        $editorder = M("exchange_order")->where("id = {$_POST['oid']} AND state = 1")->save(['state'=>2]);//核销
        if($editorder){
//        if(true){
            //给门店付款
            $o_c_id = M("exchange_order")->where("id = {$_POST['oid']}")->getField('c_id');//用户小区id
            //获取店铺店主id和店铺id
            $store_u_id = M("shop_store")->where("c_id = {$o_c_id}")->getField("u_id");//门店信息

            $voice = '';
            if($_POST['money'] != 0){
                M("member")->where('id ='.$store_u_id)->setInc('credit3',$_POST['money']);//金额
                changeCredit($store_u_id,'积分兑换核销打款','credit3','+',$_POST['money'],'3');//余额变动记录
                $voice .= $_POST['money']."元";
            }//end if

            if($_POST['money'] != 0 && $_POST['credit1'] != 0){
                $voice .= "+";
            }//end if

            if($_POST['credit1'] != 0){
                M("member")->where('id ='.$store_u_id)->setInc('credit1',$_POST['credit1']);//积分
                changeCredit($store_u_id,'积分兑换核销打款','credit1','+',$_POST['credit1'],'3');//积分变动记录
                $voice .= $_POST['credit1']."积分";
            }//end if

            sendVoice($store_u_id,'积分兑换核销收益：'.$voice);//推送系统消息

//            $this->ajaxReturn($_POST,"JSON");return;
            M("exchange_order")->where("id = {$_POST['oid']}")->save(['canceltime'=>time()]);//核销时间
            $this->ajaxReturn(['state'=>0,'val'=>"核销成功",'url'=>"http://www.xiangtws.com/index.php/Wx/User/stores.html"],"JSON");
        }else{
            $this->ajaxReturn(['state'=>1,'val'=>"核销失败"],"JSON");
        }
    }//end fun
}//end class